package com.huabo.system.service.impl;


import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.JsonBean;
import com.hbfk.util.PageInfo;
import com.huabo.system.config.DateBaseConfig;
import com.huabo.system.mysql.entity.TblFinanceDataMySql;
import com.huabo.system.mysql.mapper.TblFinanceDataMySqlMapper;
import com.huabo.system.mysql.mapper.TblOrganizationMySqlMapper;
import com.huabo.system.oracle.entity.TblFinanceData;
import com.huabo.system.oracle.mapper.TblFinanceDataMapper;
import com.huabo.system.oracle.mapper.TblOrganizationMapper;
import com.huabo.system.service.TblFinanceDataService;
import org.joda.time.Interval;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("TblFinanceDataService")
public class TblFinanceDataServiceImpl implements TblFinanceDataService {

    //private static final String jdurl= ResourceBundle.getBundle("setting/jdbc").getString("url").toString();

    @Value("${spring.datasource.oracle.url}")
    private String jdurl;

    @Resource
    private TblFinanceDataMapper tblFinanceDataMapper;
    @Resource
    private TblOrganizationMapper tblOrganizationMapper;


    @Resource
    private TblFinanceDataMySqlMapper tblFinanceDataMySqlMapper;
    @Resource
    private TblOrganizationMySqlMapper tblOrganizationMySqlMapper;


    @Override
    public Map<String, Object> del(String orderId) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);

        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            tblFinanceDataMapper.deleteByorderid(orderId);
        } else {
            tblFinanceDataMySqlMapper.deleteByorderid(orderId);
        }


        resultMap.put("code", "1");
        resultMap.put("msg", "3");
        return resultMap;
    }

    @Override
    public Map<String, Object> findByCompanyId(Integer pageNumber, String token, String staffId, Integer pageSize) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            TblStaffUtil staff = DealUserToken.parseUserToken(token);


            if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
                PageInfo<TblFinanceData> pageInfo = new PageInfo<TblFinanceData>();
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);
                pageInfo.setTlist(tblFinanceDataMapper.selectListByPageInfo(pageInfo, staff.getCurrentOrg().getOrgid()));
                pageInfo.setTotalRecord(tblFinanceDataMapper.selectCountByPageInfo(pageInfo, staff.getCurrentOrg().getOrgid()));
                resultMap.put("data", pageInfo);

            } else {
                PageInfo<TblFinanceDataMySql> pageInfo = new PageInfo<TblFinanceDataMySql>();
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);
                pageInfo.setTlist(tblFinanceDataMySqlMapper.selectListByPageInfo(pageInfo, staff.getCurrentOrg().getOrgid()));
                pageInfo.setTotalRecord(tblFinanceDataMySqlMapper.selectCountByPageInfo(pageInfo, staff.getCurrentOrg().getOrgid()));
                resultMap.put("data", pageInfo);

            }


            resultMap.put("code", "1");
            resultMap.put("msg", "数据访问成功");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public TblFinanceData get(String selectid) {
        return tblFinanceDataMapper.selectByOrderId(selectid);
    }

    @Override
    public TblFinanceDataMySql getMySql(String selectid) {
        return tblFinanceDataMySqlMapper.selectByOrderId(selectid);
    }

    @Override
    public String selectDateByCompanyid(HttpServletRequest request, String token, String staffId, TblFinanceData tlf, String parseStart, String pendDate, String fid) throws Exception {
        List<TblFinanceData> financeData = new ArrayList<>();
        //验证年份
        int prseStart = Integer.parseInt(parseStart);
        int parseEnd = Integer.parseInt(pendDate);
        Interval interval = new Interval(prseStart, parseEnd);
        List<Interval> arrayList = new ArrayList<Interval>();
        Interval inter = null;
        TblStaffUtil staff = DealUserToken.parseUserToken(token);
        BigDecimal companyid = staff.getCurrentOrg().getOrgid();


        if (tlf.getOrderId() != null) {
            financeData = tblFinanceDataMapper.selectDateByCompanyidAndOrderId(companyid, tlf.getOrderId());
        } else {
            financeData = tblFinanceDataMapper.selectDateByCompanyid(companyid);
        }
        for (TblFinanceData tblFinanceData : financeData) {
            inter = new Interval(tblFinanceData.getStartdate(), tblFinanceData.getEnddate());
            boolean contains = interval.contains(inter);
            boolean contains1 = inter.contains(interval);
            //包含
            if (prseStart < tblFinanceData.getStartdate() && parseEnd > tblFinanceData.getStartdate()) {
                return JsonBean.error("年份重复");
            }
            if (prseStart < tblFinanceData.getEnddate() && parseEnd > tblFinanceData.getEnddate()) {
                return JsonBean.error("年份重复");
            }
            if (contains == true) {
                return JsonBean.error("年份重复");
            }
            if (contains1 == true) {
                return JsonBean.error("年份重复");
            }
        }
        if (tlf != null && tlf.getOrderId() == null) {
            // 新建1
            tlf.setFid(fid);
            tlf.setDestConn(jdurl);
            tlf.setDestDbType("Oracle");
            tlf.setDestUserId("BATHDATA");
            tlf.setDestPassWord("1");
            tlf.setStartdate(prseStart);
            tlf.setEnddate(parseEnd);
            tlf.setCompanyId(companyid.toString());
            String orgname = this.tblOrganizationMapper.findByorgid(companyid);
            tlf.setCompanyName(orgname);
            tlf.setStatus(1);
            Integer saveFirstOne = tblFinanceDataMapper.saveFirst(tlf);
            //新建的账套名
            //该公司下是否有数据
            TblFinanceData selectModuleName = tblFinanceDataMapper.selectModuleName(companyid);
            String newBookName;
            if (selectModuleName != null) {
                newBookName = selectModuleName.getDestUserId();
            } else {
                newBookName = this.unRepeatSixCode() + companyid;
                int checkSchema = tblFinanceDataMapper.checkSchema(newBookName);
                if (checkSchema == 0) {
                    //tblFinanceDataMapper.createSchema(newBookName);
                    TblFinanceData tlf2 = new TblFinanceData(null, null, "Oracle", jdurl, "BATHDATA", "1", "Oracle", jdurl, newBookName, "1", companyid.toString(), staff.getCurrentOrg().getOrgname(), 2, null, null, null, null, null, null);
                    Integer saveFirstTwo = tblFinanceDataMapper.saveFirst(tlf2);
                }
            }
            //新建2
            return JsonBean.success();
        } else {
            // 修改1
            tlf.setFid(fid);
            tlf.setStartdate(prseStart);
            tlf.setEnddate(parseEnd);
            tblFinanceDataMapper.updateFin(tlf);
            return JsonBean.success();
        }

    }

    @Override
    public String selectMySqlDateByCompanyid(HttpServletRequest request, String token, String staffId, TblFinanceDataMySql tlf, String parseStart, String pendDate, String fid) throws Exception {
        List<TblFinanceDataMySql> financeData = new ArrayList<>();
        //验证年份
        int prseStart = Integer.parseInt(parseStart);
        int parseEnd = Integer.parseInt(pendDate);
        Interval interval = new Interval(prseStart, parseEnd);
        List<Interval> arrayList = new ArrayList<Interval>();
        Interval inter = null;
        TblStaffUtil staff = DealUserToken.parseUserToken(token);
        BigDecimal companyid = staff.getCurrentOrg().getOrgid();
        if (tlf.getOrderId() != null) {
            financeData = tblFinanceDataMySqlMapper.selectDateByCompanyidAndOrderId(companyid, tlf.getOrderId());
        } else {
            financeData = tblFinanceDataMySqlMapper.selectDateByCompanyid(companyid);
        }
        for (TblFinanceDataMySql tblFinanceData : financeData) {
            inter = new Interval(tblFinanceData.getStartdate(), tblFinanceData.getEnddate());
            boolean contains = interval.contains(inter);
            boolean contains1 = inter.contains(interval);
            //包含
            if (prseStart < tblFinanceData.getStartdate() && parseEnd > tblFinanceData.getStartdate()) {
                return JsonBean.error("年份重复");
            }
            if (prseStart < tblFinanceData.getEnddate() && parseEnd > tblFinanceData.getEnddate()) {
                return JsonBean.error("年份重复");
            }
            if (contains == true) {
                return JsonBean.error("年份重复");
            }
            if (contains1 == true) {
                return JsonBean.error("年份重复");
            }
        }
        if (tlf != null && tlf.getOrderId() == null) {
            // 新建1
            tlf.setFid(fid);
            tlf.setDestConn(jdurl);
            tlf.setDestDbType("Oracle");
            tlf.setDestUserId("BATHDATA");
            tlf.setDestPassWord("1");
            tlf.setStartdate(prseStart);
            tlf.setEnddate(parseEnd);
            tlf.setCompanyId(companyid.toString());
            String orgname = this.tblOrganizationMySqlMapper.findByorgidname(companyid);
            tlf.setCompanyName(orgname);
            tlf.setStatus(1);
            Integer saveFirstOne = tblFinanceDataMySqlMapper.saveFirst(tlf);
            //新建的账套名
            //该公司下是否有数据
            TblFinanceDataMySql selectModuleName = tblFinanceDataMySqlMapper.selectModuleName(companyid);
            String newBookName;
            if (selectModuleName != null) {
                newBookName = selectModuleName.getDestUserId();
            } else {
                newBookName = this.unRepeatSixCode() + companyid;
                int checkSchema = tblFinanceDataMySqlMapper.checkSchema(newBookName);
                if (checkSchema == 0) {
                    //tblFinanceDataMapper.createSchema(newBookName);
                    TblFinanceDataMySql tlf2 = new TblFinanceDataMySql(null, null, "Oracle", jdurl, "BATHDATA", "1", "Oracle", jdurl, newBookName, "1", companyid.toString(), staff.getCurrentOrg().getOrgname(), 2, null, null, null, null, null, null);
                    Integer saveFirstTwo = tblFinanceDataMySqlMapper.saveFirst(tlf2);
                }
            }
            //新建2
            return JsonBean.success();
        } else {
            // 修改1
            tlf.setFid(fid);
            tlf.setStartdate(prseStart);
            tlf.setEnddate(parseEnd);
            tblFinanceDataMySqlMapper.updateFin(tlf);
            return JsonBean.success();
        }
    }

    @Override
    public String findModelName(Integer type, BigDecimal orgid) {


        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            return tblFinanceDataMapper.selectName(type, orgid);
        } else {
            return tblFinanceDataMySqlMapper.selectName(type, orgid);
        }


    }

    /**
     * 登录页管理--生成路径
     *
     * @return
     */
    public String unRepeatSixCode() {
        String sixChar = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
        Date date = new Date();
        String time = sdf.format(date);
        for (int i = 0; i < time.length() / 2; i++) {
            String singleChar;
            String x = time.substring(i * 2, (i + 1) * 2);
            int b = Integer.parseInt(x);
            if (b < 10) {
                singleChar = Integer.toHexString(Integer.parseInt(x));
            } else if (b >= 10 && b < 36) {
                singleChar = String.valueOf((char) (Integer.parseInt(x) + 55));
            } else {
                singleChar = String.valueOf((char) (Integer.parseInt(x) + 61));
            }
            sixChar = sixChar + singleChar;

        }
        System.out.println("生成一个6位不可重复的字符编码是：" + sixChar);
        return sixChar;
    }
}
